EFFECT.Exp = {}
EFFECT.Exp[1] = Sound("ambient/explosions/explode_4.wav")
EFFECT.Exp[2] = Sound("ambient/explosions/explode_9.wav")

function EFFECT:Init(data)
	
	self.Entity:SetPos(data:GetOrigin())
	self.Origin = data:GetOrigin()
	self.Scale = data:GetMagnitude()
	self.SphereScale = data:GetScale()
	
	self.LifeTime = 15
	self.ShockWaveTime = 8
	self.ShockSphereTime = 0.5
	self.GenTime = CurTime()
	
	self.Entity:SetModel("models/zup/shields/200_shield.mdl")
	
	local ModelSize = (self.Entity:OBBMaxs() - self.Entity:OBBCenter()).z
	self.ModelScale = self.SphereScale/ModelSize
	
	self.Color = Color(255, 140, 0, 255)
	
	self.Mat = {}
	
	self.Mat.Flare = Material("Sprites/glow01")
	self.Mat.Flare:SetMaterialInt("$spriterendermode",5)
	self.Mat.Flare:SetMaterialInt("$illumfactor",8)
	
	self.Mat.Core = Material("Sprites/light_glow01")
	self.Mat.Core:SetMaterialInt("$spriterendermode",5)
	self.Mat.Core:SetMaterialInt("$illumfactor",8)
	
	self.Mat.Wave = Material("CDE/shock_halo_ring")
	self.Mat.Wave:SetMaterialInt("$spriterendermode",5)
	self.Mat.Wave:SetMaterialInt("$illumfactor",8)
	
	self.Mat.Explosion = Material("models/props/cs_office/clouds")
	
	self.particles = {}
	
	--WorldSound(self.Exp[math.random(1,2)], self.Origin, 100, 100) --TODO:BETTER EXPLOSION SOUNDS
	--self.Velocity = self.Prop:GetPhysicsObject():GetVelocity()
	
		local emitter = ParticleEmitter(self.Entity:GetPos())
		for i=1,(self.Scale/5) do
			local particle = emitter:Add("Sprites/glow01", self.Entity:GetPos())
			particle:SetColor(255, 140, 0, 255)
			particle:SetVelocity(self.Entity:RndNormalVector() * self.Scale * 4)
			particle:SetDieTime(self.LifeTime * 2)
			particle:SetLifeTime(math.Rand(0, self.LifeTime))
			particle:SetStartSize(math.Rand(0, self.Scale/2))
			particle:SetEndSize(0)
			particle:SetStartAlpha(255)
			particle:SetEndAlpha(0)
		end
		
		local detonator = ParticleEmitter(self.Entity:GetPos())
		for i=1,(self.Scale/10) do
			local spawnvector = self.Entity:RndNormalVector() * self.Scale
			local particle = detonator:Add("particles/flamelet" .. math.random(1,5), self.Entity:GetPos() + spawnvector)
			particle:SetColor(255, 140, 0, 255)
			particle:SetVelocity(spawnvector / 4)
			particle:SetDieTime(self.LifeTime)
			particle:SetLifeTime(0)
			particle:SetStartSize(math.Rand(self.Scale / 2, self.Scale))
			particle:SetEndSize(self.Scale)
			particle:SetStartAlpha(255)
			particle:SetEndAlpha(0)
			table.insert(self.particles, particle)
		end
	
	self.MainSize = 1
	self.WaveSize = 1
	self.SphereSize = 0
	
end

function EFFECT:RndNormalVector()
	local Vect = Vector()
	Vect.x = math.Rand(-1,1)
	Vect.y = math.Rand(-1,1)
	Vect.z = math.Rand(-1,1)
	return Vect
end

function EFFECT:Think()
	if ((self.GenTime + self.LifeTime) < CurTime()) then
		return false
	else
		self.MainSize = math.Clamp(1 - ((CurTime() - self.GenTime) / self.LifeTime), 0, 1)
		self.WaveSize = math.Clamp(((CurTime() - self.GenTime) / self.ShockWaveTime), 0, 1)
		self.SphereSize = math.Clamp(((CurTime() - self.GenTime) / self.ShockSphereTime), 0, 1)
		self.Entity:SetColor(self.Color.r, self.Color.g, self.Color.b, math.Clamp((1 - self.SphereSize) * 255, 1, 255))
		return true
	end
end

function EFFECT:Render()
	
	--Lens Flare Effect
	render.SetMaterial(self.Mat.Flare)
	render.DrawSprite(self.Entity:GetPos(), self.MainSize * self.Scale * 120, self.MainSize * self.Scale * 16, Color(0, 0, 255, self.MainSize * 100))
	
	--Core Light Effect
	render.SetMaterial(self.Mat.Core)
	render.DrawSprite(self.Entity:GetPos(), self.MainSize * self.Scale * 64, self.MainSize * self.Scale * 64, Color(255, 140, 0, self.MainSize * 255))
	
	--Shockwave
	local shockwavesize = self.WaveSize
	render.SetMaterial(self.Mat.Wave)
	render.DrawSprite(self.Entity:GetPos(), shockwavesize * self.Scale * 32, shockwavesize * self.Scale * 32, Color(255, 140, 0, (1 - self.WaveSize) * 255))
	
	--Shocksphere
	if (self.SphereSize != 1) then
		local size = self.ModelScale * self.SphereSize
		SetMaterialOverride(self.Mat.Explosion)
		self.Entity:SetModelScale(Vector()*size)
		self.Entity:DrawModel()
		SetMaterialOverride()
	end
end
